﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Web;

namespace Common
{
    /// <summary>
    /// 文件操作辅助类
    /// </summary>
    public class FileUtil
    {
        /// <summary>
        /// 删除单个文件
        /// </summary>
        /// <param name="filePath">文件相对路径</param>
        public static void Delete(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }
            string fullpath = PathUtil.MapPath(filePath);
            if (File.Exists(fullpath))
            {
                File.Delete(fullpath);
            }
        }

        /// <summary>
        /// 删除指定目录下的所有文件
        /// </summary>
        /// <param name="filePath"></param>
        public static void DeleteAllFiles(string filePath)
        {
            string fullpath = PathUtil.MapPath(filePath);
            DirectoryInfo dic = new DirectoryInfo(fullpath);
            if (dic.Exists)
            {
                FileInfo[] fileArr = dic.GetFiles();
                if (fileArr.Length > 0)
                {
                    foreach (FileInfo file in fileArr)
                    {
                        file.Delete();
                    }
                }
            }
        }

        /// <summary>
        /// 检查是否为合法的上传文件
        /// </summary>
        public static bool CheckFileExt(string _fileExt, string allowExts)
        {
            string[] allowExt = allowExts.Split(',');
            for (int i = 0; i < allowExt.Length; i++)
            {
                if (allowExt[i].ToLower() == _fileExt.ToLower()) { return true; }
            }
            return false;
        }

        /// <summary>
        /// 是否为图片文件
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        public static bool IsImage(string _fileExt)
        {
            ArrayList al = new ArrayList();
            al.Add("bmp");
            al.Add("jpeg");
            al.Add("jpg");
            al.Add("gif");
            al.Add("png");
            if (al.Contains(_fileExt.ToLower()))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 检查文件大小是否合法
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        /// <param name="_fileSize">文件大小(KB)</param>
        public static bool CheckFileSize(string _fileExt, int _fileSize, int allowMaxSize)
        {
            //判断是否为图片文件
            if (IsImage(_fileExt))
            {
                if (allowMaxSize > 0 && _fileSize > allowMaxSize * 1024)
                {
                    return false;
                }
            }
            else
            {
                if (allowMaxSize > 0 && _fileSize > allowMaxSize * 1024)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="sourceFileName">原始文件路径含文件名的相对路径</param>
        /// <param name="destFilePath">目标文件相对路径</param>
        public static string Move(string sourceFileName, string destFilePath)
        {
            if (File.Exists(PathUtil.MapPath(sourceFileName)))
            {
                string originalFileName = sourceFileName.Substring(sourceFileName.LastIndexOf("/") + 1); //取得文件原名
                string toFileFullPath = PathUtil.MapPath(destFilePath);
                if (!Directory.Exists(toFileFullPath))
                {
                    Directory.CreateDirectory(toFileFullPath);
                }

                File.Move(PathUtil.MapPath(sourceFileName), toFileFullPath + originalFileName);
                return destFilePath + originalFileName;
            }
            return string.Empty;
        }

        /// <summary>
        /// 读取指定文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string Read(string filePath)
        {
            try
            {
                return File.ReadAllText(PathUtil.MapPath(filePath), Encoding.UTF8);
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}
